<?php
    class Member {
    	private $m_dbConnection = null;
		
		// Tabellnamn
		private $m_memberTableName = "member";
		private $m_boatTableName = "boatRegister";
		
        public function __construct(DBConnection $a_connection) {
                $this->m_dbConnection = $a_connection;
        }
		
		public function CreateMember($m_ssn, $m_firstName, $m_lastName) {
			
			$sql = "INSERT INTO $this->m_memberTableName (m_memberId, m_ssn, m_firstName, m_lastName) VALUES (NULL, ?, ?, ?)";
			$stmt = $this->m_dbConnection->Prepare($sql); // Analyserar och förbereder sql-frågan
			$stmt->bind_param("sss", $m_ssn, $m_firstName, $m_lastName); // Binder parametrar ? ? i sql-frågan
			
			if($stmt->execute()) { // Om exekveringen lyckas
				$stmt->close(); 
				return true;
			} else {
				$stmt->close();
				return false;
			}
			// Ta bort!
			$dbconnection->Close();  			
		}
		
		public function AddBoatInfo($m_memberId, $m_length, $m_type) {
			$sql = "INSERT INTO $this->m_boatTableName (m_boatId, m_type, m_length, m_memberId) VALUES(NULL, ?, ?, ?)";
			$stmt = $this->m_dbConnection->Prepare($sql);
			$stmt->bind_param("sii", $m_type, $m_length, $m_memberId);		
			if($stmt->execute()) {
				$stmt->close();
				return true;
			} else {
				$stmt->close();
				return false;
			}
		}
		
		public function ChangeMember($m_memberId, $m_firstName, $m_lastName, $m_ssn) {
			$sql = "UPDATE $this->m_memberTableName SET m_firstName=?, m_lastName=?, m_ssn=? WHERE m_memberId=?";
			$stmt = $this->m_dbConnection->Prepare($sql);
			$stmt->bind_param("sssi", $m_firstName, $m_lastName, $m_ssn, $m_memberId);
			
			if($stmt->execute()) {
				$stmt->close();
				return true;
			} else {
				$stmt->close();
				return false;
			}
		}
    
		public function DeleteMember($m_memberId) {
			$sql = "DELETE FROM $this->m_memberTableName WHERE m_memberId=?";
			$stmt = $this->m_dbConnection->Prepare($sql);
			
			$stmt->bind_param("i", $m_memberId);
			
			if($stmt->execute()) {
				$stmt->close();
				return true;
			} else {
				$stmt->close();
				return false;
			}
		}


				
		public function GetStaticMemberList() {
			$sql = "SELECT m.m_memberId as Medlemsnr, m.m_firstName as Förnamn, m.m_lastName as Efternamn,
			 				COUNT(b.m_boatId) as Båtar FROM $this->m_memberTableName as m
							LEFT JOIN $this->m_boatTableName as b ON m.m_memberId = b.m_memberId
							GROUP BY m.m_memberId 
						";

			return $this->RunAndFetchObjects($sql);
		}
		
		public function GetFullMemberList() {
			$sql = "SELECT m.m_memberId as Medlemsnr, m.m_firstName as Förnamn, m.m_lastName as Efternamn, m.m_ssn as Personnr, 
					b.m_boatId as Båtnummer, b.m_type as Båttyp, b.m_length as Båtlängd
					FROM $this->m_memberTableName as m
					LEFT JOIN $this->m_boatTableName as b ON
							m.m_memberId = b.m_memberId";
							
			$stmt = $this->m_dbConnection->Prepare($sql);
			
			return $this->RunAndFetchObjects($sql);
		}
		
		public function GetMemberById($memberId) {
			$sql = "SELECT m.m_memberId as Medlemsnummer, m.m_ssn as Personnummer, m.m_firstName as Förnamn, m.m_lastName as Efternamn
					FROM $this->m_memberTableName as m WHERE m_memberId=$memberId";
			return $this->RunAndFetchObjects($sql);
		}
		
		 private function RunAndFetchObjects($a_stmt) {
                return $this->m_dbConnection->RunAndFetchObjects($a_stmt, "Member");
        }
	}
?>